package com.ggh.jinjilive.ui.social.near.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemLiveListBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.social.near.NearLive;
import com.bumptech.glide.Glide;

import java.util.List;

public class NearLiveAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<NearLive> nearLiveList;


    public void setAdd(List<NearLive> nearLiveList) {
        this.nearLiveList = nearLiveList;
        notifyDataSetChanged();
    }

    public void Add(List<NearLive> nearLiveList) {
        if(null != this.nearLiveList){
            this.nearLiveList.addAll(nearLiveList);
        }else {
            this.nearLiveList = nearLiveList;
        }
        notifyDataSetChanged();
    }


    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemLiveListBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_live_list, parent, false);
        return new ItemViewHolder(binding);
    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
        if(recyclerViewholder instanceof ItemViewHolder){
            ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
            NearLive nearLive = nearLiveList.get(position);
            if (null != nearLive) {
                Glide.with(holder.binding.RoundedImageView.getContext()).load(nearLive.getCover()).into(holder.binding.RoundedImageView);
                holder.binding.tvNumber.setText(nearLive.getNumber() +"");
                if (nearLive.getType() == 0){
                    holder.binding.roomType.setText("普通");
                }else if (nearLive.getType() == 1){
                    holder.binding.roomType.setText("密码");
                }else if (nearLive.getType() == 2){
                    holder.binding.roomType.setText("付费");
                }else if (nearLive.getType() == 3) {
                    holder.binding.roomType.setText("计时");
                }
                holder.binding.tvName.setText(nearLive.getTitle());
                holder.binding.tvName.setSelected(true);
                holder.binding.RoundedImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(null != addItemOnClickListener){
                            addItemOnClickListener.onSkipClicked(position,nearLive);
                        }
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return null == nearLiveList ? 0 : nearLiveList.size();
    }




    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemLiveListBinding binding;

        public ItemViewHolder(@NonNull ItemLiveListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            ViewGroup.LayoutParams params =  binding.llLive.getLayoutParams();
            params.height = ScreenUtils.getScreenWidth() / 2 - SizeUtils.dp2px(15);
            binding.llLive.setLayoutParams(params);
        }
    }

}